package atmGUI;

import util.JDBCUtil;
import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class TransferPanel extends JPanel implements ActionListener {
    private Icon transferImage = Util.getIcon("/image/pane.jpg");//转账按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATM_GUI_Main frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel transferUserJLabel;//转账用户提示插件
    private JTextField iptTransferUser;//转账用户输入框
    private JLabel transferMoneyJLabel;//转账金额提示插件
    private JTextField iptTransferMoney;//转账金额输入框
    private JButton transferBtn;//转账按钮
    private JLabel transferJLabel;
    private double transferMoney;
    private Font font = new Font("宋体", Font.PLAIN, 20);

    public TransferPanel(ATM_GUI_Main frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //转账用户提示插件
        transferUserJLabel = new JLabel("输入收款人账户：");
        transferUserJLabel.setBounds(240, 190, 150, 50);
        transferUserJLabel.setFont(font);
        this.add(transferUserJLabel);
        //转账用户输入框
        iptTransferUser = new JTextField();
        iptTransferUser.setBounds(380, 200, 300, 30);
        iptTransferUser.setFont(font);
        this.add(iptTransferUser);
        //转账金额提示插件
        transferMoneyJLabel = new JLabel("输入转账金额：");
        transferMoneyJLabel.setBounds(240, 240, 150, 50);
        transferMoneyJLabel.setFont(font);
        this.add(transferMoneyJLabel);
        //转账金额输入框
        iptTransferMoney = new JTextField();
        iptTransferMoney.setBounds(380, 250, 300, 30);
        iptTransferMoney.setFont(font);
        this.add(iptTransferMoney);
        //转账按钮
        transferJLabel = new JLabel("转  账");
        transferJLabel.setBounds(900, 220, 140, 50);
        transferJLabel.setFont(new Font("宋体", Font.BOLD, 25));
        transferJLabel.setForeground(Color.white);
        this.add(transferJLabel);
        transferBtn = new JButton(transferImage);
        transferBtn.setBounds(870, 220, 140, 50);
        transferBtn.setOpaque(false);
        this.add(transferBtn);
        transferBtn.addActionListener(this);
        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == transferBtn) {
            String otherAccount = iptTransferUser.getText();
            JDBCUtil jdbcUtil = new JDBCUtil();
            User loginUser = jdbcUtil.getById(frame.getLoginUser().getId());
            User otherUser = jdbcUtil.getByUserAccount(otherAccount);
            try {
                transferMoney = Double.parseDouble(iptTransferMoney.getText()); //输入转账的金额
            } catch (NumberFormatException exception) {
                JOptionPane.showMessageDialog(this, "输入金额为空，或格式错误");
                return;
            }
            // 判断账号是否存在
            if (otherUser == null) {
                JOptionPane.showMessageDialog(this, "查无此人");
                return;
            }
            // 判断是否转给自己
            if (loginUser.getAccount().equals(otherAccount)) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                return;
            }
            //判断余额是否为100的倍数
            if (transferMoney % 100 != 0) {
                JOptionPane.showMessageDialog(this, "请输入100的倍数");
                return;
            }
            // 判断余额是否足够
            if (transferMoney > loginUser.getBalance()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
            boolean bl = jdbcUtil.testTransition(loginUser,otherUser);
            if (bl){
                // 当期账户扣钱
                loginUser.setBalance(loginUser.getBalance() - transferMoney);
                // 对方账户加钱
                otherUser.setBalance(otherUser.getBalance() + transferMoney);
                jdbcUtil.update(loginUser);
                jdbcUtil.update(otherUser);
                JOptionPane.showMessageDialog(this, "转账成功");
                iptTransferUser.setText("");
                iptTransferMoney.setText("");
                frame.switchPanel("menu");
            }else {
                return;
            }
        } else if (e.getSource() == backBtn) {
            frame.switchPanel("menu");
            iptTransferUser.setText("");
            iptTransferMoney.setText("");
        }
    }
}



